package com.guanzhi.creation.builder.stream;

/**
 * 设计模式
 * <p> Package: com.guanzhi.creation.builder.stream </p>
 * <p> File: Computer.java </p>
 * <p> Description:  (创建型模式-工厂模式-建造者模式)</p>
 * <p> Date: 2024/4/14 2:02</p>
 *
 * @author 观之
 * @version 1.0 （代码版本）
 * @email <a href="mailto:guanzhi55634@aliyun.com">观之</a>（邮箱）
 * @date 2024/4/14 2:02
 * @since jdk17（jdk版本）
 */
// 产品类：电脑配置
public class Computer {
    private String cpu;
    private String ram;
    private int storage;
    private boolean hasGraphicsCard;

    // 私有构造函数，确保只能通过建造者来创建
    private Computer(ComputerBuilder builder) {
        this.cpu = builder.cpu;
        this.ram = builder.ram;
        this.storage = builder.storage;
        this.hasGraphicsCard = builder.hasGraphicsCard;
    }

    // 获取电脑配置信息
    public String getConfigInfo() {
        return "CPU: " + cpu + ", RAM: " + ram + " GB, Storage: " + storage + " GB, Graphics Card: " + (hasGraphicsCard ? "Yes" : "No");
    }

    // 建造者抽象类
    public abstract static class ComputerBuilder {
        protected String cpu;
        protected String ram;
        protected int storage;
        protected boolean hasGraphicsCard;

        // 设置CPU
        public ComputerBuilder withCpu(String cpu) {
            this.cpu = cpu;
            return this;
        }

        // 设置RAM
        public ComputerBuilder withRam(String ram) {
            this.ram = ram;
            return this;
        }

        // 设置存储空间
        public ComputerBuilder withStorage(int storage) {
            this.storage = storage;
            return this;
        }

        // 是否包含独立显卡
        public ComputerBuilder withGraphicsCard(boolean hasGraphicsCard) {
            this.hasGraphicsCard = hasGraphicsCard;
            return this;
        }

        // 抽象的构建方法
        public abstract Computer build();
    }

    // 具体建造者类
    public static class BasicComputerBuilder extends ComputerBuilder {
        @Override
        public Computer build() {
            return new Computer(this);
        }
    }

    // 客户端代码
    public static void main(String[] args) {
        ComputerBuilder basicBuilder = new BasicComputerBuilder()
                .withCpu("Intel Core i5")
                .withRam("8GB")
                .withStorage(512)
                .withGraphicsCard(false);

        Computer computer = basicBuilder.build();
        System.out.println(computer.getConfigInfo());
    }
}
